﻿/*
 Create by skybot
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.MemoryMappedFiles;

namespace InCom.Core.TerminalSend.MappedFile
{
    /// <summary>
    /// 基础数据类型,不能处理string
    /// 
    /// </summary>
    public class BaseDataType<T>
        where T : struct
    {

        protected T val;

        /// <summary>
        /// 数据值
        /// </summary>
        public T Value
        {
            get
            {
                //读取int 
                Access.Read<T>(Offset, out val);
                return val;
            }
            set
            {

                val = value;
                //写入共享文件
                Access.Write<T>(Offset, ref val);

            }
        }
        /// <summary>
        /// 创建一个新的基于内存文件的int
        /// </summary>
        /// <param name="offset">偏移量</param>
        /// <param name="access">映射对像</param>
        public BaseDataType(long offset, MemoryMappedViewAccessor access)
        {
            Access = access;
            Offset = offset;
        }

        /// <summary>
        /// 视图
        /// </summary>
        protected MemoryMappedViewAccessor Access { get; set; }

        /// <summary>
        /// 基于文件映射的依稀量
        /// </summary>
        public long Offset { get; protected set; }
    }
}
